package com.js.data.system.service.impl;

import com.js.data.system.entity.SysDictItem;
import com.js.data.system.mapper.SysDictItemMapper;
import com.js.data.system.service.DictItemService;
import com.js.data.system.vo.DictItemVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 数据字典详情 服务实现类
 * </p>
 *
 * @author wujj
 * @since 2022-09-06
 */
@Service
public class DictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDictItem> implements DictItemService {


    /**
     * 根据keyName查询
     *
     * @param key keyName
     * @return List<SysDictItem>
     */
    @Override
    public List<SysDictItem> listByKeyType(String key) {
        LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictItem::getKeyType, key);
        return list(queryWrapper);
    }

    /**
     * 根据keyName查询
     *
     * @param keys keyName
     * @return List<SysDictItem>
     */
    @Override
    public List<SysDictItem> listByKeyTypes(List<String> keys) {
        LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysDictItem::getKeyType, keys);
        return list(queryWrapper);
    }

    /**
     * 更新数据项keyName
     *
     * @param dictId  数据字典id
     * @param keyType keyType
     */
    @Override
    public void updateKeyTypeByDictId(Long dictId, String keyType) {
        LambdaUpdateWrapper<SysDictItem> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysDictItem::getDictId, dictId);
        SysDictItem sysDictItem = new SysDictItem();
        sysDictItem.setKeyType(keyType);
        update(sysDictItem, updateWrapper);
    }

    /**
     * 根据字典id删除
     *
     * @param dictId 数据字典id
     */
    @Override
    public void removeByDictId(Long dictId) {
        lambdaUpdate().eq(SysDictItem::getDictId, dictId).remove();
    }

    /**
     * 数据字典项
     *
     * @param dictId 字典id
     * @return List<DictItemVO>
     */
    @Override
    public List<DictItemVO> listDictItem(Long dictId) {
        List<DictItemVO> retList = new ArrayList<>();
        LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictItem::getDictId, dictId);
        List<SysDictItem> dictList = this.list(queryWrapper);
        dictList.forEach(dataDict -> {
            DictItemVO dictItemVO = new DictItemVO();
            dictItemVO.setLabel(dataDict.getLabel());
            dictItemVO.setValue(dataDict.getValue());
            dictItemVO.setSortBy(dataDict.getSortBy());
            retList.add(dictItemVO);
        });
        return retList;
    }
}
